package com.showjoy.job.starter;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

import com.showjoy.job.starter.properties.JobProperties;
import com.xxl.job.core.executor.XxlJobExecutor;

@Configuration
@EnableConfigurationProperties({ JobProperties.class })
public class JobAutoConfiguration {
	private static final Logger logger = LoggerFactory.getLogger(JobAutoConfiguration.class);
	@Resource
	private JobProperties jobProperties;

	@Bean(initMethod = "start", destroyMethod = "destroy")
	public XxlJobExecutor xxlJobExecutor() {
		if(StringUtils.isEmpty(this.jobProperties.getAdminAddresses())){
			logger.error("XXXXXXXXXXXXXXXXXX注册调度中心不能为空XXXXXXXXXXXXXXXXXX");
			return null;
		}
		if(StringUtils.isEmpty(this.jobProperties.getAppName())){
			logger.error("XXXXXXXXXXXXXXXXXXappName不能为空XXXXXXXXXXXXXXXXXX");
			return null;
		}
		this.log();
		XxlJobExecutor xxlJobExecutor = new XxlJobExecutor();
		xxlJobExecutor.setAdminAddresses(this.jobProperties.getAdminAddresses());
		xxlJobExecutor.setAppName(this.jobProperties.getAppName());
		xxlJobExecutor.setIp(this.jobProperties.getIp());
		xxlJobExecutor.setPort(this.jobProperties.getPort());
		xxlJobExecutor.setAccessToken(this.jobProperties.getAccessToken());
		xxlJobExecutor.setLogPath(this.jobProperties.getLogPath());
		xxlJobExecutor.setLogRetentionDays(this.jobProperties.getLogRetentionDays());
		return xxlJobExecutor;
	}

	private void log() {
		logger.info(">>>>>>>>>>> xxl-job config init.");
		logger.info("xxl-job config adminAddresses:{}", this.jobProperties.getAdminAddresses());
		logger.info("xxl-job config appName: {}", this.jobProperties.getAppName());
		logger.info("xxl-job config ip: {}", this.jobProperties.getIp());
		logger.info("xxl-job config port: {}", this.jobProperties.getPort());
		logger.info("xxl-job config accessToken: {}", this.jobProperties.getAccessToken());
		logger.info("xxl-job config logPath: {}", this.jobProperties.getLogPath());
		logger.info("xxl-job config logRetentionDays: {}", this.jobProperties.getLogRetentionDays());
	
	}
}
